<template>
  <div class="register-container">
    <!-- 动态粒子背景 -->
    <div class="particles-container">
      <div 
        v-for="(particle, index) in particles" 
        :key="index"
        class="particle"
        :style="particle.style"
      ></div>
    </div>
    
    <!-- 浮动元素 -->
    <div class="floating-elements">
      <div class="floating-circle circle-1"></div>
      <div class="floating-circle circle-2"></div>
      <div class="floating-circle circle-3"></div>
    </div>
    
    <div class="register-box">
      <div class="register-header">
        <div class="logo-container">
          <el-icon size="50" class="register-icon neon-icon"><School /></el-icon>
          <div class="logo-glow"></div>
        </div>
        <h2 class="register-title">注册新账号</h2>
        <p class="register-subtitle">请填写您的注册信息</p>
      </div>
      
      <el-form
        ref="registerFormRef"
        :model="registerForm"
        :rules="registerRules"
        class="register-form"
        @submit.prevent="handleRegister"
      >
        <el-form-item prop="useraccount">
          <el-input
            v-model="registerForm.useraccount"
            placeholder="请输入用户账号"
            prefix-icon="User"
            size="large"
            class="neon-input"
          />
        </el-form-item>
        
        <el-form-item prop="username">
          <el-input
            v-model="registerForm.username"
            placeholder="请输入用户名"
            prefix-icon="Avatar"
            size="large"
            class="neon-input"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            size="large"
            show-password
            class="neon-input"
          />
        </el-form-item>
        
        <el-form-item prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            placeholder="请确认密码"
            prefix-icon="Key"
            size="large"
            show-password
            class="neon-input"
            @keyup.enter="handleRegister"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            class="register-button neon-button"
            :loading="loading"
            @click="handleRegister"
          >
            <span class="button-text">注册</span>
            <div class="button-glow"></div>
          </el-button>
        </el-form-item>
        
        <el-form-item>
          <el-button
            size="large"
            class="back-button neon-button-secondary"
            @click="goToLogin"
          >
            <span class="button-text">返回登录</span>
            <div class="button-glow"></div>
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { School, User, Avatar, Lock, Key } from '@element-plus/icons-vue'
import { register } from '@/api/auth'

export default {
  name: 'Register',
  setup() {
    const router = useRouter()
    const registerFormRef = ref()
    const loading = ref(false)
    
    const registerForm = reactive({
      useraccount: '',
      username: '',
      password: '',
      confirmPassword: ''
    })
    
    // 自定义确认密码验证规则
    const validateConfirmPassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请确认密码'))
      } else if (value !== registerForm.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    
    const registerRules = {
      useraccount: [
        { required: true, message: '请输入用户账号', trigger: 'blur' },
        { min: 3, max: 20, message: '用户账号长度在 3 到 20 个字符', trigger: 'blur' },
        { pattern: /^[a-zA-Z0-9_]+$/, message: '用户账号只能包含字母、数字和下划线', trigger: 'blur' }
      ],
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 2, max: 20, message: '用户名长度在 2 到 20 个字符', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' },
        { pattern: /^(?=.*[a-zA-Z])(?=.*\d)/, message: '密码必须包含字母和数字', trigger: 'blur' }
      ],
      confirmPassword: [
        { required: true, validator: validateConfirmPassword, trigger: 'blur' }
      ]
    }
    
    // 粒子系统
    const particles = ref([])
    let particleInterval = null
    
    const createParticle = () => {
      return {
        style: {
          left: Math.random() * 100 + '%',
          top: Math.random() * 100 + '%',
          width: Math.random() * 4 + 2 + 'px',
          height: Math.random() * 4 + 2 + 'px',
          animationDelay: Math.random() * 2 + 's',
          animationDuration: (Math.random() * 3 + 2) + 's'
        }
      }
    }
    
    const initParticles = () => {
      particles.value = Array.from({ length: 50 }, createParticle)
      
      // 动态添加粒子
      particleInterval = setInterval(() => {
        particles.value.push(createParticle())
        if (particles.value.length > 100) {
          particles.value.shift()
        }
      }, 200)
    }
    
    const handleRegister = async () => {
      if (!registerFormRef.value) return
      
      await registerFormRef.value.validate((valid) => {
        if (valid) {
          performRegister()
        }
      })
    }
    
    const performRegister = async () => {
      loading.value = true
      
      try {
        // 调用后端注册API
        const response = await register({
          useraccount: registerForm.useraccount,
          username: registerForm.username,
          password: registerForm.password,
          role: 'USER' // 默认角色为普通用户
        })
        
        if (response.success) {
          ElMessage.success('注册成功！即将跳转到登录页面...')
          // 注册成功后跳转到登录页面
          setTimeout(() => {
            router.push('/login')
          }, 1500)
        } else {
          ElMessage.error(response.message || '注册失败')
        }
      } catch (error) {
        ElMessage.error('注册失败，请稍后重试')
        console.error('注册错误：', error)
      } finally {
        loading.value = false
      }
    }
    
    const goToLogin = () => {
      router.push('/login')
    }
    
    onMounted(() => {
      initParticles()
    })
    
    onUnmounted(() => {
      if (particleInterval) {
        clearInterval(particleInterval)
      }
    })
    
    return {
      registerFormRef,
      registerForm,
      registerRules,
      loading,
      handleRegister,
      goToLogin,
      particles,
      School,
      User,
      Avatar,
      Lock,
      Key
    }
  }
}
</script>

<style scoped>
.register-container {
  height: 100vh;
  background: linear-gradient(-45deg, #667eea, #764ba2, #f093fb, #f5576c);
  background-size: 400% 400%;
  animation: gradientShift 15s ease infinite;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
}

@keyframes gradientShift {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

/* 粒子动画 */
.particles-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

.particle {
  position: absolute;
  background: rgba(255, 255, 255, 0.8);
  border-radius: 50%;
  animation: float 3s ease-in-out infinite;
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
}

@keyframes float {
  0%, 100% { transform: translateY(0px) rotate(0deg); opacity: 1; }
  50% { transform: translateY(-20px) rotate(180deg); opacity: 0.5; }
}

/* 浮动元素 */
.floating-elements {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 0;
}

.floating-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  animation: floatCircle 20s infinite linear;
}

.circle-1 {
  width: 100px;
  height: 100px;
  top: 10%;
  left: 10%;
  animation-duration: 25s;
}

.circle-2 {
  width: 150px;
  height: 150px;
  top: 60%;
  right: 15%;
  animation-duration: 30s;
  animation-direction: reverse;
}

.circle-3 {
  width: 80px;
  height: 80px;
  bottom: 20%;
  left: 70%;
  animation-duration: 20s;
}

@keyframes floatCircle {
  0% { transform: translate(0, 0) rotate(0deg); }
  25% { transform: translate(50px, -30px) rotate(90deg); }
  50% { transform: translate(-30px, -50px) rotate(180deg); }
  75% { transform: translate(-50px, 30px) rotate(270deg); }
  100% { transform: translate(0, 0) rotate(360deg); }
}

/* 注册框 */
.register-box {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 20px;
  padding: 40px;
  width: 400px;
  max-width: 90vw;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 1;
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo-container {
  position: relative;
  display: inline-block;
  margin-bottom: 15px;
}

.register-icon {
  color: white;
  filter: drop-shadow(0 0 10px rgba(255, 255, 255, 0.5));
}

.logo-glow {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 80px;
  height: 80px;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.3) 0%, transparent 70%);
  border-radius: 50%;
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { transform: translate(-50%, -50%) scale(1); opacity: 0.5; }
  50% { transform: translate(-50%, -50%) scale(1.2); opacity: 0.8; }
}

.register-title {
  color: white;
  font-size: 28px;
  font-weight: bold;
  margin: 0 0 10px 0;
  text-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
}

.register-subtitle {
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  margin: 0;
}

.register-form {
  margin-top: 20px;
}

/* 霓虹输入框 */
.neon-input :deep(.el-input__wrapper) {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 10px;
  box-shadow: none;
  transition: all 0.3s ease;
}

.neon-input :deep(.el-input__wrapper):hover {
  border-color: rgba(255, 255, 255, 0.5);
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.2);
}

.neon-input :deep(.el-input__wrapper).is-focus {
  border-color: #409eff;
  box-shadow: 0 0 15px rgba(64, 158, 255, 0.5);
}

.neon-input :deep(.el-input__inner) {
  color: white;
  font-size: 14px;
}

.neon-input :deep(.el-input__inner)::placeholder {
  color: rgba(255, 255, 255, 0.6);
}

.neon-input :deep(.el-input__prefix) {
  color: rgba(255, 255, 255, 0.8);
}

/* 霓虹按钮 */
.register-button {
  width: 100%;
  background: linear-gradient(45deg, #409eff, #67c23a);
  border: none;
  border-radius: 10px;
  color: white;
  font-weight: bold;
  font-size: 16px;
  padding: 12px 24px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(64, 158, 255, 0.4);
}

.register-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(64, 158, 255, 0.6);
}

.back-button {
  width: 100%;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: white;
  transition: all 0.3s ease;
}

.back-button:hover {
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.5);
  transform: translateY(-2px);
}

.neon-button {
  position: relative;
  overflow: hidden;
}

.neon-button .button-glow {
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
  transition: left 0.5s;
}

.neon-button:hover .button-glow {
  left: 100%;
}

.neon-button-secondary {
  position: relative;
  overflow: hidden;
}

.neon-button-secondary .button-glow {
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

.neon-button-secondary:hover .button-glow {
  left: 100%;
}

.button-text {
  position: relative;
  z-index: 1;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .register-box {
    padding: 30px 20px;
    width: 95vw;
  }
  
  .register-title {
    font-size: 24px;
  }
}
</style>